#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2011/06/01

'''

import gdata.spreadsheet
import string
from UserCommentDetail import UserCommentDetail
from google.appengine.ext import db

class UserCommentKeywordData(db.Model):
    date = db.StringProperty(required=True, multiline=False)
    word1 = db.StringProperty(multiline=False)
    score1 = db.StringProperty(multiline=False)
    word2 = db.StringProperty(multiline=False)
    score2 = db.StringProperty(multiline=False)
    word3 = db.StringProperty(multiline=False)
    score3 = db.StringProperty(multiline=False)
    word4 = db.StringProperty(multiline=False)
    score4 = db.StringProperty(multiline=False)
    word5 = db.StringProperty(multiline=False)
    score5 = db.StringProperty(multiline=False)
    word6 = db.StringProperty(multiline=False)
    score6 = db.StringProperty(multiline=False)
    word7 = db.StringProperty(multiline=False)
    score7 = db.StringProperty(multiline=False)
    word8 = db.StringProperty(multiline=False)
    score8 = db.StringProperty(multiline=False)
    word9 = db.StringProperty(multiline=False)
    score9 = db.StringProperty(multiline=False)
    word10 = db.StringProperty(multiline=False)
    score10 = db.StringProperty(multiline=False)
    word11 = db.StringProperty(multiline=False)
    score11 = db.StringProperty(multiline=False)
    word12 = db.StringProperty(multiline=False)
    score12 = db.StringProperty(multiline=False)
    word13 = db.StringProperty(multiline=False)
    score13 = db.StringProperty(multiline=False)
    word14 = db.StringProperty(multiline=False)
    score14 = db.StringProperty(multiline=False)
    word15 = db.StringProperty(multiline=False)
    score15 = db.StringProperty(multiline=False)
    word16 = db.StringProperty(multiline=False)
    score16 = db.StringProperty(multiline=False)
    word17 = db.StringProperty(multiline=False)
    score17 = db.StringProperty(multiline=False)
    word18 = db.StringProperty(multiline=False)
    score18 = db.StringProperty(multiline=False)
    word19 = db.StringProperty(multiline=False)
    score19 = db.StringProperty(multiline=False)
    word20 = db.StringProperty(multiline=False)
    score20 = db.StringProperty(multiline=False)

class UserCommentKeyword:
    columnHeader = [u'日付',\
                    u'単語1', u'スコア1',\
                    u'単語2', u'スコア2',\
                    u'単語3', u'スコア3',\
                    u'単語4', u'スコア4',\
                    u'単語5', u'スコア5',\
                    u'単語6', u'スコア6',\
                    u'単語7', u'スコア7',\
                    u'単語8', u'スコア8',\
                    u'単語9', u'スコア9',\
                    u'単語10', u'スコア10',\
                    u'単語11', u'スコア11',\
                    u'単語12', u'スコア12',\
                    u'単語13', u'スコア13',\
                    u'単語14', u'スコア14',\
                    u'単語15', u'スコア15',\
                    u'単語16', u'スコア16',\
                    u'単語17', u'スコア17',\
                    u'単語18', u'スコア18',\
                    u'単語19', u'スコア19',\
                    u'単語20', u'スコア20']
    key ='0AgnpWHfnlnQbdGZIVDN6UUZ3WnFvZVhZZ0NoUGdKMHc'
    wksht_id = ''
    # output spread sheet
    def outformat(self, line):
        result = []
        result.append("<tr>")
        for value in line:
            result.append("<td>")
            result.append(value)
            result.append("</td>")
        result.append("</tr>\n")
            
    def outCommentKeyword(self, gd_client):
        def PrintFeed(feed):
            result = []
            for entry in feed.entry:
                if isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed):
#                    print '%s %s %s' % (i, entry.title.text, entry.content.text)
                    date = entry.title.text
                    # Print this row's value for each column (the custom dictionary is
                    # built from the gsx: elements in the entry.) See the description of
                    # gsx elements in the protocol guide.
                    result.append('<tr>')
                    for i, key in enumerate(self.columnHeader):
                        word = entry.custom[key].text
                        if key != self.columnHeader[0] and i % 2 != 0:
                            url = './detail?date="%s"&word="%s"' % (date, word)
                            result.append('<td nowrap><a href=%s>' % url)
                            result.append(entry.custom[key].text)
                            result.append('<a></td>')
                        else:
                            result.append('<td>')
                            result.append(entry.custom[key].text)
                            result.append('</td>')
                    
                    result.append("</tr>\n")
            return "".join(result)
        
        def ListGetAction(gd_client, key, wksht_id):
            # Get the list feed
            feed = gd_client.GetListFeed(key, wksht_id)
            result = PrintFeed(feed)
            return result
        
        def CellsGetAction(gd_client, key, wksht_id):
            # Get the feed of cells
            feed = gd_client.GetCellsFeed(key, wksht_id)
            PrintFeed(feed)

        result = ListGetAction(gd_client, self.key, self.wksht_id)
#        result = ListGetAction(gd_client, UserCommentDetail.key, 'od6')
        return result

    # udpate database
    def updateCommentKeyword(self, gd_client):
        feed = gd_client.GetListFeed(self.key, self.wksht_id)
        datas = UserCommentKeywordData.all()
        for data in datas:
            data.delete()

        maxDate = None
        minDate = None        
        for entry in feed.entry:
            if isinstance(feed, gdata.spreadsheet.SpreadsheetsListFeed):
#                    print '%s %s %s' % (i, entry.title.text, entry.content.text)
                date = entry.title.text
                if maxDate is None:
                    maxDate = date
                minDate = date
                # Print this row's value for each column (the custom dictionary is
                # built from the gsx: elements in the entry.) See the description of
                # gsx elements in the protocol guide.
                # すんげぇ馬鹿みたいなロジック。死にたい。
                keywordData = UserCommentKeywordData(date = date)
                word = entry.custom[self.columnHeader[1]].text
                keywordData.word1 = word
                score = entry.custom[self.columnHeader[2]].text
                keywordData.score1 = score
                word = entry.custom[self.columnHeader[3]].text
                keywordData.word2 = word
                score = entry.custom[self.columnHeader[4]].text
                keywordData.score2 = score
                word = entry.custom[self.columnHeader[5]].text
                keywordData.word3 = word
                score = entry.custom[self.columnHeader[6]].text
                keywordData.score3 = score
                word = entry.custom[self.columnHeader[7]].text
                keywordData.word4 = word
                score = entry.custom[self.columnHeader[8]].text
                keywordData.score4 = score
                word = entry.custom[self.columnHeader[9]].text
                keywordData.word5 = word
                score = entry.custom[self.columnHeader[10]].text
                keywordData.score5 = score
                word = entry.custom[self.columnHeader[11]].text
                keywordData.word6 = word
                score = entry.custom[self.columnHeader[12]].text
                keywordData.score6 = score
                word = entry.custom[self.columnHeader[13]].text
                keywordData.word7 = word
                score = entry.custom[self.columnHeader[14]].text
                keywordData.score7 = score
                word = entry.custom[self.columnHeader[15]].text
                keywordData.word8 = word
                score = entry.custom[self.columnHeader[16]].text
                keywordData.score8 = score
                word = entry.custom[self.columnHeader[17]].text
                keywordData.word9 = word
                score = entry.custom[self.columnHeader[18]].text
                keywordData.score9 = score
                word = entry.custom[self.columnHeader[19]].text
                keywordData.word10 = word
                score = entry.custom[self.columnHeader[20]].text
                keywordData.score10 = score
                keywordData.word11 = word
                score = entry.custom[self.columnHeader[21]].text
                keywordData.score11 = score
                word = entry.custom[self.columnHeader[22]].text
                keywordData.word12 = word
                score = entry.custom[self.columnHeader[23]].text
                keywordData.score12 = score
                word = entry.custom[self.columnHeader[24]].text
                keywordData.word13 = word
                score = entry.custom[self.columnHeader[25]].text
                keywordData.score13 = score
                word = entry.custom[self.columnHeader[26]].text
                keywordData.word14 = word
                score = entry.custom[self.columnHeader[27]].text
                keywordData.score14 = score
                word = entry.custom[self.columnHeader[28]].text
                keywordData.word15 = word
                score = entry.custom[self.columnHeader[29]].text
                keywordData.score15 = score
                word = entry.custom[self.columnHeader[30]].text
                keywordData.word16 = word
                score = entry.custom[self.columnHeader[31]].text
                keywordData.score16 = score
                word = entry.custom[self.columnHeader[32]].text
                keywordData.word17 = word
                score = entry.custom[self.columnHeader[33]].text
                keywordData.score17 = score
                word = entry.custom[self.columnHeader[34]].text
                keywordData.word18 = word
                score = entry.custom[self.columnHeader[35]].text
                keywordData.score18 = score
                word = entry.custom[self.columnHeader[36]].text
                keywordData.word19 = word
                score = entry.custom[self.columnHeader[37]].text
                keywordData.score19 = score
                word = entry.custom[self.columnHeader[38]].text
                keywordData.word20 = word
                score = entry.custom[self.columnHeader[39]].text
                keywordData.score20 = score
                keywordData.put()
        return maxDate, minDate
        
    def __init__(self, id):
        self.wksht_id = id